﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;

namespace ProductManagement
{
    public partial class ThuVo : Form
    {
        GiaoHangDTO giaoHang;
        DataTable dtVoGas;
        public ThuVo()
        {
            InitializeComponent();
        }
        public ThuVo(GiaoHangDTO _giaoHang)
        {
            InitializeComponent();
            giaoHang = _giaoHang;
        }
        private void NhanVo_Load(object sender, EventArgs e)
        {
            try
            {
                // Fill data into column DaiLy
                String strQuery = "select dl.ID as ID, dl.TenDaiLy as TenDaiLy from DaiLy dl inner join PhanPhoi pp on dl.ID = pp.DaiLy where pp.MaGiaoHang=" + giaoHang.Id + " group by dl.ID, dl.TenDaiLy";
                DataTable dt = DataAccess.Query(strQuery);
                (dgvVoBinhGas.Columns[1] as DataGridViewComboBoxColumn).DataSource = dt;
                (dgvVoBinhGas.Columns[1] as DataGridViewComboBoxColumn).DisplayMember = "TenDaiLy";
                (dgvVoBinhGas.Columns[1] as DataGridViewComboBoxColumn).ValueMember = "ID";
                strQuery = "select * from LoaiGas";
                DataTable dt2 = DataAccess.Query(strQuery);
                DataRow newRow = dt2.NewRow();
                newRow["ID"] = 0;
                newRow["TenLoai"] = "Thế chân";
                dt2.Rows.InsertAt(newRow, 0);
                (dgvVoBinhGas.Columns[3] as DataGridViewComboBoxColumn).DataSource = dt2;
                (dgvVoBinhGas.Columns[3] as DataGridViewComboBoxColumn).DisplayMember = "TenLoai";
                (dgvVoBinhGas.Columns[3] as DataGridViewComboBoxColumn).ValueMember = "ID";
                dgvVoBinhGas.Rows[0].Cells[0].Value = 1;

                strQuery = "select pp.DaiLy, loai.ID as ID, loai.TenLoai &' (' & SUM(pp.SoLuong) & ')' as LoaiGas, SUM(pp.SoLuong) as SoLuong  from PhanPhoi pp left join LoaiGas loai on pp.LoaiGas=loai.ID where and pp.MaGiaoHang =" + giaoHang.Id + " group by loai.ID, loai.TenLoai, pp.DaiLy";

                dtVoGas = DataAccess.Query(strQuery);
                (dgvVoBinhGas.Columns[2] as DataGridViewComboBoxColumn).DataSource = dtVoGas;
                (dgvVoBinhGas.Columns[2] as DataGridViewComboBoxColumn).DisplayMember = "LoaiGas";
                (dgvVoBinhGas.Columns[2] as DataGridViewComboBoxColumn).ValueMember = "ID";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể load form\r\n" + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Close();
            }
        }
        private String KiemTraThuVo()
        {
            String mess = "";
            int i = 0;
            int n = dgvVoBinhGas.Rows.Count;
            if (n == 1)
                mess += "Chưa có dữ liệu\r\n";
            for (i = 0; i < n; i++)
            {
                if (i != n - 1)
                {
                    if (dgvVoBinhGas.Rows[i].Cells[1].Value == null || dgvVoBinhGas.Rows[i].Cells[2].Value == null || dgvVoBinhGas.Rows[i].Cells[3].Value == "" ||
                        dgvVoBinhGas.Rows[i].Cells[4].Value == "")
                        mess += "Dữ liệu ở dòng " + (i + 1) + " chưa đúng\r\n";
                    int id = (int)dgvVoBinhGas.Rows[i].Cells[2].Value;
                    long soGiao = 0;
                    foreach (DataRow aRow in dtVoGas.Rows)
                        if ((int)aRow["ID"] == id)
                            soGiao = (long)((double)aRow["SoLuong"]);
                    if (soGiao < long.Parse(dgvVoBinhGas.Rows[i].Cells[4].Value.ToString()))
                    {
                        mess += "Số lượng ở dòng " + (i + 1) + " vượt quá mức cho phép";
                    }
                }
            }
            return mess;
        }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                String strError = KiemTraThuVo();
                if (strError == "")
                {
                    ThuHoiDTO thuHoi = new ThuHoiDTO();
                    foreach (DataGridViewRow aRow in dgvVoBinhGas.Rows)
                    {
                        if (aRow.Cells[1].Value != null)
                        {
                            thuHoi.DaiLy = (int)aRow.Cells[1].Value;
                            thuHoi.VoPhanPhoi = (int)aRow.Cells[2].Value;
                            thuHoi.VoThuVe = (int)aRow.Cells[3].Value;
                            thuHoi.SoLuong = int.Parse(aRow.Cells[4].Value as String);
                            thuHoi.PhiDoiVo = float.Parse(aRow.Cells[5].Value as String);
                            thuHoi.MaGiaoHang = giaoHang.Id;
                            ThuHoiDAO.InsertThuHoi(thuHoi);
                        }
                    }
                    MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                    this.Close();
                }
                else
                    MessageBox.Show(strError, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin ở các cột", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow aRow in dgvVoBinhGas.Rows)
            {
                if (aRow.Selected == true)
                {
                    dgvVoBinhGas.Rows.Remove(aRow);
                }
            }
            dgvVoBinhGas_UserDeletedRow(sender, null);
        }

        private void btnXoaTatCa_Click(object sender, EventArgs e)
        {
            dgvVoBinhGas.Rows.Clear();
            dgvVoBinhGas_UserDeletedRow(sender, null);
        }

        private void dgvVoBinhGas_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {            
            dgvVoBinhGas.Rows[dgvVoBinhGas.Rows.Count - 1].Cells[0].Value = dgvVoBinhGas.Rows.Count;
        }

        private void dgvVoBinhGas_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {
            for (int i = 0; i < dgvVoBinhGas.Rows.Count; i++)
                dgvVoBinhGas.Rows[i].Cells[0].Value = i + 1;
        }

        private void dgvVoBinhGas_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {            
            try
            {
                if (e.ColumnIndex == 1 && e.RowIndex > -1)
                {
                    int idDaiLy = (int)dgvVoBinhGas.Rows[e.RowIndex].Cells[1].Value;
                    // Fill data into column LoaiGas
                    String strQuery = "select pp.DaiLy, loai.ID as ID, loai.TenLoai &' (' & SUM(pp.SoLuong) & ')' as LoaiGas, SUM(pp.SoLuong) as SoLuong  from PhanPhoi pp left join LoaiGas loai on pp.LoaiGas=loai.ID where pp.DaiLy =" + idDaiLy + " and pp.MaGiaoHang =" + giaoHang.Id + " group by loai.ID, loai.TenLoai, pp.DaiLy";
 
                    dtVoGas = DataAccess.Query(strQuery);
                    (dgvVoBinhGas.Columns[2] as DataGridViewComboBoxColumn).DataSource = dtVoGas;
                    (dgvVoBinhGas.Columns[2] as DataGridViewComboBoxColumn).DisplayMember = "LoaiGas";
                    (dgvVoBinhGas.Columns[2] as DataGridViewComboBoxColumn).ValueMember = "ID";
                   

                }
                if (e.ColumnIndex == 3 && e.RowIndex >= 0 && e.RowIndex <= dgvVoBinhGas.Rows.Count - 1)
                {
                    if ((int)dgvVoBinhGas.Rows[e.RowIndex].Cells[3].Value == 0)
                        dgvVoBinhGas.Rows[e.RowIndex].Cells[5].Value = "0";
                    else
                        dgvVoBinhGas.Rows[e.RowIndex].Cells[5].Value = null;
                }
                if (e.ColumnIndex == 4)
                {
                    int loaiGas = (int)dgvVoBinhGas.Rows[e.RowIndex].Cells[2].Value;
                    int soLuong = int.Parse(dgvVoBinhGas.Rows[e.RowIndex].Cells[4].Value.ToString());
                    int tonKho = 0;
                    foreach (DataRow aRow in dtVoGas.Rows)
                    {
                        if ((int)aRow["ID"] == loaiGas)
                        {
                            tonKho = (int)aRow["SoLuong"];
                            break;
                        }
                    }
                    if (tonKho < soLuong)
                    {
                        MessageBox.Show("Số lượng vượt quá mức cho phép", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        dgvVoBinhGas.Rows[e.RowIndex].Cells[4].Value = 0;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnBack_Click_1(object sender, EventArgs e)
        {
            Close();
        }
    }
}
